<?php

class AreasController extends AppController{
    public $layout='correspondencia';
    public $uses=array('Area','User');
    
    public function index(){
        $areas= $this->Area->find('all',array('recursive'=>-1,'limit'=>50, 'order'=>array('Area.id DESC')));
        $this->set(compact('areas'));
    }
     public function insertar(){
         if(!empty($this->request->data)){
             $this->Area->create();
             if($this->Area->save($this->request->data)){
                 $this->Session->setFlash('Guardado con exito',msgbueno);
                 $this->redirect(array('action'=>'index'),null,true);
             }else{
                 $this->Session->setFlash('Error al guardar',msgerror);
             }
         }
     }
     
     public function editar($id=null){
         $this->Area->id=$id;
         if(!$this->Area->exists()) {
            throw new NotFoundException(__('Invalido'));
        }
        if(!empty($this->request->data)){
            $this->Area->id=$id;
            if($this->Area->save($this->request->data)){
                $this->Session->setFlash('Modificacion exitosa',msgbueno);
                $this->redirect(array('action'=>'index'),null,true);
            }else{
                $this->Session->setFlash('No se efectuo la modificacion!',msgerror);
                $this->redirect(array('action'=>'index'),null,true);
            }
        }else{
            $this->request->data =  $this->Area->read(null,$id);
        }
     }
     
     public function eliminar($id){
         if($this->request->data('post')){
             throw new MethodNotAllowedException();
         }
         if($this->Area->delete($id)){
             $this->Session->setFlash('Eliminacion exitosa',msgbueno);
             $this->redirect(array('action'=>'index'),null,true);
         }
     }
}
?>
